<?php

function unsCalendar_createCalendarsForFormation($id) {
    $workPraticeGroups = unsCalendar_getWorkPraticeGroupDetailForFormation($id);
    foreach ($workPraticeGroups as $workPraticeGroup) {
        $calendar = unsCalendar_getCalendarForWorkPraticeGroup($workPraticeGroup['workPraticeGroup_id']);
        if ($calendar == 0)
            unsCalendar_createCalendarForWorkPraticeGroup($workPraticeGroup);
    }
}

function unsCalendar_createCalendarForWorkPraticeGroup($workPraticeGroup) {
    global $babDB, $babBody;

    $nameCalendar = $workPraticeGroup['formation_name'] . '_' . $workPraticeGroup['tutorialGroup_name'] . '_' . $workPraticeGroup['workPraticeGroup_name'];

    if (unsCalendar_getPublicCalendarIDForName($workPraticeGroup['workPraticeGroup_name']) == 0) {

        $babDB->db_query("insert into " . BAB_CAL_PUBLIC_TBL . " (name, description, id_dgowner, idsa) values ('" . $nameCalendar . "', '" . $nameCalendar . "', '" . $babDB->db_escape_string($babBody->currentAdmGroup) . "', '0')");

        $idowner = $babDB->db_insert_id();

        $babDB->db_query("insert into " . BAB_CALENDAR_TBL . " (owner, type) values ('" . $babDB->db_escape_string($idowner) . "', '" . BAB_CAL_PUB_TYPE . "')");
        
        $idCal = $babDB->db_insert_id();
        //echo "insert into ".UNSCALENDAR_CLASSROOMS_TABLE_NAME." values(null, '$name', ".$idowner." , $buildingID, $videoprojector, $capacity, now(), ". $GLOBALS['BAB_SESS_USERID'] . " );";
        $babDB->db_query("insert into " . UNSCALENDAR_WORKPRATICEGROUP_CALENDARS_TABLE_NAME . " values (null, " . $workPraticeGroup['workPraticeGroup_id'] . ", " . $idCal . " );");
    } else {
        $babBody->addError(bab_translate("A classroom with this name allready exist !", $GLOBALS['babAddonFolder']));
    }
}

function unsCalendar_deleteCalendarForWorkPraticeGroup($id) {
    global $babDB;

    $calendar = unsCalendar_deleteCalendarForWorkPraticeGroup($id);
    if ($calendar != 0) {
        bab_deleteCalendar($calendar['calendarID']);
        $babDB->db_query("delete from " . UNSCALENDAR_WORKPRATICEGROUP_CALENDARS_TABLE_NAME . " where id=" . $calendar['id']);
    }
}

function unsCalendar_getCalendarForWorkPraticeGroup($id) {
    global $babDB;

    $res = $babDB->db_query("select id, workPraticeGroupCalendar_workPraticeGroupID as workPraticeGroupID, workPraticeGroupCalendar_calendarID as calendarID from " . UNSCALENDAR_WORKPRATICEGROUP_CALENDARS_TABLE_NAME . " where workPraticeGroupCalendar_workPraticeGroupID = " . $id);

    if ($babDB->db_num_rows($res) == 1) {
        $calendar = $babDB->db_fetch_array($res);
        return $calendar;
    } else
        return 0;
}

function unsCalendar_getWorkPraticeGroupDetailForFormation($id) {
    global $babDB;
    
    $res = $babDB->db_query("select * from " . UNSCALENDAR_WORK_PRATICE_GROUPS_VIEW_NAME . " where formation_id = " . $id);

    $workPraticeGroups = array();
    $i = 0;
    while ($arr = $babDB->db_fetch_array($res)) {
        $workPraticeGroups[$i] = $arr;
        $i++;
    }
    return $workPraticeGroups;
}

?>
